<?php
/**
##### Controller.php - This is the main controller of the control panel. 
**/
error_reporting(0);
require ("../CONFIG.config");
require (LIB . "common/common.class.php");
require (LIB . "common/db.class.php");
require ("../DB.config");

$clsDBConn = new DBConn();
$smarty->debugging = false;
$smarty->caching = true;
$smarty->cache_lifetime = 120;

CommonFunc::loadFile(ADAPTOR . "/Adminpaginator.php");


$task = $_GET['task'];
switch ($task)
{
	case base64_encode('authuser') :
		CommonFunc::loadFile('controller/UserController.php');
		$dbconn = $clsDBConn->DBConn();
		$clsUser = new User();
		$request = CommonFunc::getRequest();
		$username = $request['username'];
		$password = $request['password'];		
		$authUser = $clsUser->authUser($username, $password);
		break;
		
	case base64_encode('loadmodule') :
		$request = CommonFunc::getRequest();		
		$url = $request['url'];
		$url = explode("/", $url);
		$module = $url[0];
		$action = $url[1];
		$param1 = $url[2];
		//sleep(1);
		switch ($module) 
		{
			case 'language' :		
				CommonFunc::loadFile('controller/LanguageController.php');
				if($action == "editlang")
				{
					$clsLanguage = new Language();
					$clsLanguage->editLang();
				} else {
					$clsLanguage = new Language();
					$clsLanguage->index();
				}	
				break;
				
			case 'category' :	
				CommonFunc::loadFile('controller/CategoryController.php');
				$clsCategory = new Category();
				$clsCategory->index();
				break;
			
			case 'gallery' :
				CommonFunc::loadFile('controller/GalleryController.php');				
				$clsGallery = new Gallery();
				$clsGallery->index();
				break;
                       case 'promobanner' :
				CommonFunc::loadFile('controller/PromobannerController.php');
				$clsPromobanner = new Promobanner();
				$clsPromobanner->index();
				break;
				
			case 'news' :
				CommonFunc::loadFile('controller/NewsController.php');				
				if($action == "addnews")
				{
					$clsNews = new News();
					$clsNews->addNews();
				} else if ($action == "editnews") {
					$clsNews = new News();
					$clsNews->editNews();
				} {				
					$clsNews = new News();
					$clsNews->index();
				}	
				break;
				
			case "product" :
				CommonFunc::loadFile('controller/ProductController.php');				
				if($action == "addproduct")
				{
					$clsProduct = new Product();
					$clsProduct->addProduct();
				} else if($action == "editproduct") {
					$clsProduct = new Product();
					$clsProduct->editProduct();
				} else if ($action == "addimage") {
					$clsProduct = new Product();
					$clsProduct->addProductImage();
				} else if ($action == "addfeatures") {
					$clsProduct = new Product();
					$clsProduct->addFeatures();
				} else if ($action == "homepageproduct") {
					$clsProduct = new Product();
					$clsProduct->homepageProducts();
				} else {
					$clsProduct = new Product();
					$clsProduct->index();						
				}	
				break;
                         case "order" :
				CommonFunc::loadFile('controller/OrderController.php');
				if($action == "processorderbak")
				{
					$clsOrder = new Order();
					$clsProduct->processOrder();
				}else if($action == "view")
				{
					$clsOrder = new Order();
					$clsOrder->view();
				}else {
					$clsOrder = new Order();
					$clsOrder->index();
				}
				break;
           	case "user" :
				CommonFunc::loadFile('controller/UserController.php');
				if($action == "adduser")
				{
					$clsUser = new User();
					$clsUser->addUser();
				} else if($action == "edituser") {
					$clsUser = new User();
					$clsUser->editUser();
				} 
                                else {
					$clsUser = new User();
					$clsUser->index();
				}
				break;
                        case "creditor" :
				CommonFunc::loadFile('controller/CreditorController.php');
				if($action == "viewcreditaccount")
				{
					$clsUser = new Creditor();
					$clsUser->viewcreditaccount();
				} else if($action == "edituser") {
					$clsUser = new User();
					$clsUser->editUser();
				}
                                else {
					$clsCreditor = new Creditor();
					$clsCreditor->index();
				}
				break;
				
			case "settings" :
				CommonFunc::loadFile('controller/SettingsController.php');
				$clsSettings = new Settings();
				$clsSettings->index();
				
				break;
		}		
		
		break;

	case base64_encode('addcategory') :
		CommonFunc::loadFile('model/Category.php');
		$request = CommonFunc::getRequest();
		$modCat = new ModelCategory();
		$modCat->addCategory($request);		
		
		break;
       case base64_encode('checkitemcode') :
		CommonFunc::loadFile('model/Product.php');
		$request = CommonFunc::getRequest();
		$modProd = new ModelProduct();
		$modProd->checkitemcode($request);

		break;
       case 'checkitemcode' :
		CommonFunc::loadFile('model/Product.php');
		$request = CommonFunc::getRequest();
		$modProd = new ModelProduct();
		$modProd->checkitemcode($request);

		break;

       case base64_encode('cancelorder') :
        //    print_r($request);exit;
		CommonFunc::loadFile('model/Order.php');
		$request = CommonFunc::getRequest();
		$modOrder = new ModelOrder();
		$modOrder->cancelorder($request);
                break;
        case base64_encode('checkitemcodeupdate') :
		CommonFunc::loadFile('model/Product.php');
		$request = CommonFunc::getRequest();
		$modProd = new ModelProduct();
		$modProd->checkitemcodeupdate($request);

		break;
		
	case "addgallery" :		
		CommonFunc::loadFile('model/Image.php');
		CommonFunc::loadFile('model/Gallery.php');
		$modGallery = new ModelGallery();
		$image = new SimpleImage();
   		$uploaddir = '../images/banners/'; 
		$file = $uploaddir . basename($_FILES['uploadfile']['name']); 
		//print_r($_FILES);
		if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
			$image->load($file);
   			
   			$image->resize(700, 240); // larger image resize
   			$image->save($uploaddir . basename($_FILES['uploadfile']['name'])); 
			$image->resize(175, 60);
   			$image->save($uploaddir . 'thumbs/' . basename($_FILES['uploadfile']['name']));  
   			$modGallery->addGallery(basename($_FILES['uploadfile']['name']));
			//echo "success"; 
		} else {
			echo "error";
		}
		
		break;
       case "addpromobanner" :
		CommonFunc::loadFile('model/Image.php');
		CommonFunc::loadFile('model/Promobanner.php');
		$modPromobanner = new ModelPromobanner();
		$image = new SimpleImage();
   		$uploaddir = '../images/promobanner/';
		$file = $uploaddir . basename($_FILES['uploadfile']['name']);
		//print_r($_FILES);
		if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
			$image->load($file);
   			$image->resize(700, 240); // larger image resize
   			$image->save($uploaddir . basename($_FILES['uploadfile']['name']));
			$image->resize(236,100);
   			$image->save($uploaddir . 'thumbs/' . basename($_FILES['uploadfile']['name']));
   			$modPromobanner->addpromobanner(basename($_FILES['uploadfile']['name']));
			//echo "success";
		} else {
			echo "error";
		}

		break;
		
	case "addproductimage" :		
		CommonFunc::loadFile('model/Image.php');
		CommonFunc::loadFile('model/Product.php');
		$modGallery = new ModelProduct();
		$image = new SimpleImage();
   		$uploaddir = '../images/products/'; 
		$file = $uploaddir . basename($_FILES['uploadfile']['name']); 
		$request = CommonFunc::getRequestGET();
		$productid = $_GET['productid'];
		if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
			list($width, $height, $type, $attr) = getimagesize(basename($_FILES['uploadfile']['name']));
			$imgwidth = $width;
			$imgheight = $height; ;
			
			$image->load($file);   			
   			if($imgwidth >= 500)
   			{
   				$image->resizeToWidth(400);
   			} else if($imgheight >= 500) {
   				$image->resizeToHeight(400);
   			}
			
			//$image->resize(445, 400); // larger image resize
   			$image->save($uploaddir . basename($_FILES['uploadfile']['name'])); 
			
   			if($width > $height)
   			{
   				$image->resizeToWidth(100); 
   			} else {
   				$image->resizeToHeight(100); 
   			}
   			$image->save($uploaddir . 'thumbs/' . basename($_FILES['uploadfile']['name']));  
   			$modGallery->addProductImage($productid, basename($_FILES['uploadfile']['name']));			
		} else {
			echo "error";
		}
		
		break;		
		
	case base64_encode('browsegallery') :
		CommonFunc::loadFile('model/Gallery.php');
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		$smarty = new Smarty();
		$modGallery = new ModelGallery();
		
		$gallery_arr = $modGallery->getGalleryImages();
		$smarty->assign("gallery_arr", $gallery_arr);
		$smarty->display(ADMINTPL . 'gallery/browse.tpl');
		break;	
		
	case base64_encode("delgalleryimage") :
		CommonFunc::loadFile('model/Gallery.php');
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		$request = CommonFunc::getRequest();
		$smarty = new Smarty();
		$modGallery = new ModelGallery();
		$del_image = $modGallery->delGalleryImage($request['gallery_id']);
		break;
      case  "delpromoimage" :
		CommonFunc::loadFile('model/Promobanner.php');
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		$request = CommonFunc::getRequest();
		$smarty = new Smarty();
		$modPromo = new ModelPromobanner();
		$del_image = $modPromo->delPromoImage($request['promo_id']);
		break;
       case base64_encode("deletecategory") :
		CommonFunc::loadFile('model/Category.php');
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		$request = CommonFunc::getRequest();
		$smarty = new Smarty();
		$modCategory = new ModelCategory();
		$del_cat= $modCategory->deletecategory($request);
		break;
         case  "deletehomepageproduct"  :
		CommonFunc::loadFile('model/Product.php');
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		$request = CommonFunc::getRequest();
		$smarty = new Smarty();
		$modProduct = new ModelProduct();
		$del_Product= $modProduct->deletehomepageproduct($request);
		break;
        case base64_encode("deleteproduct") :
		CommonFunc::loadFile('model/Product.php');
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		$request = CommonFunc::getRequest();
		$smarty = new Smarty();
		$modProduct = new ModelProduct();
		$del_Product= $modProduct->deleteproduct($request);
		break;
        case "sorthomepage" :
		CommonFunc::loadFile('model/Product.php');
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		$request = CommonFunc::getRequest();
                parse_str($request['products'], $productOrder);
		$modProduct = new ModelProduct();
		$sorthomepage= $modProduct->sorthomepage($productOrder);
		break;
         case "addhomepageproduct" :
		CommonFunc::loadFile('model/Product.php');
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
                $request = CommonFunc::getRequest();
		$modProduct = new ModelProduct();
		$addhomepageproduct= $modProduct->addhomepageproduct($request);
		break;
            
        case base64_encode("updatecredit") :
		CommonFunc::loadFile('model/Creditor.php');
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		$request = CommonFunc::getRequest();
		$smarty = new Smarty();
		$modCreditor = new ModelCreditor();
		$updatecredit= $modCreditor->updatecredit($request);
		break;
		
	case base64_encode("delproductimage") :
		CommonFunc::loadFile('model/Product.php');
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		$request = CommonFunc::getRequest();
		$smarty = new Smarty();
		$modProduct = new ModelProduct();
		$del_image = $modProduct->delProductImage($request['productid'],$request['image_id']);
		break;	
		
	case base64_encode("updatelanguage") :
		CommonFunc::loadFile('controller/LanguageController.php');
		$request = CommonFunc::getRequest();
		$clsLanguage = new Language();
		$clsLanguage->updateLanguage($request);
		break;
		
	case base64_encode("savenews") :
		CommonFunc::loadFile('controller/NewsController.php');
		$request = CommonFunc::getRequest();
		$clsNews = new News();
		$clsNews->saveNews();
		break;	
		
	case base64_encode("getsubcat") :
		CommonFunc::loadFile('controller/ProductController.php');
		$request = CommonFunc::getRequest();
		$clsProduct = new Product();
		$clsProduct->getSubCat();
		break;
		
	case base64_encode("updateproduct") :
		CommonFunc::loadFile('model/Product.php');
		$request = CommonFunc::getRequest();
		$modProduct = new ModelProduct();
		$modProduct->updateProduct($request);
		break;
       case base64_encode("updateuser") :
		CommonFunc::loadFile('model/User.php');
		$request = CommonFunc::getRequest();
		$modUser = new ModelUser();
		$modUser->updateUser($request);
		break;
       case base64_encode("saveproduct") :
		CommonFunc::loadFile('model/Product.php');
		$request = CommonFunc::getRequest();
		$modProduct = new ModelProduct();
		$modProduct->saveProduct($request);
		break;
        case base64_encode("createsearchstr") :
		CommonFunc::loadFile('model/Product.php');
		$modProduct = new ModelProduct();
		$modProduct->createsearchstr();
		break;
        case base64_encode("savefeatures") :
              
		CommonFunc::loadFile('model/Product.php');
		$request = CommonFunc::getRequest();
              //  print_r($request);exit;
		$modProduct = new ModelProduct();
                $elementarray=array();
                $structkeyopt=array();
                $structkey=array();
                $i=0;
                foreach($request as $x){
                  foreach($x as $y){
                      switch($y["name"]){
                          case "structkeyopt":
                              $structkeyopt[]=$y["value"];
                              break;
                          case "structkey":
                              $structkey[]=$y["value"];
                               break;
                          case "structval":
                              $structval[]=$y["value"];
                               break;
                          case "productid":
                              $elementarray["productid"]=$y["value"];
                           case "catid":
                              $elementarray["catid"]=$y["value"];

                      }
                  }
                }

                foreach($structkeyopt as $structkeyoptkey=>$structkeyoptval){
                    $elementarray["structkeyopt"][$structkeyoptkey]=$structkeyoptval;
                    $elementarray["structkey"][$structkeyoptkey]=$structkey[$structkeyoptkey];
                    $elementarray["structval"][$structkeyoptkey]=$structval[$structkeyoptkey];
                }
		$modProduct->saveFeaturesbyProductId($elementarray);
		break;
       case base64_encode("deletefeature") :
		CommonFunc::loadFile('model/Product.php');
		$request = CommonFunc::getRequest();
		$modProduct = new ModelProduct();
		$modProduct->deleteFeaturesbyProductId($request);
               // header( 'Location:product/index') ;
		break;
		
	case base64_encode("updatenews") :
		CommonFunc::loadFile('model/News.php');
		$request = CommonFunc::getRequest();
		$modNews = new ModelNews();
		$modNews->updateNews($request);
		break;
       case base64_encode("updategalurl") :
		CommonFunc::loadFile("model/Gallery.php");
                $request = CommonFunc::getRequest();
		$modGal = new ModelGallery($request);
		$modGal->updateimageurl($request);
		break;
	 case  "updatepromourl" :
		CommonFunc::loadFile("model/Promobanner.php");
                $request = CommonFunc::getRequest();
		$modpromo = new ModelPromobanner($request);
		$modpromo->updatepromourl($request);
		break;
        case  "updatepromonewwindow" :
		CommonFunc::loadFile("model/Promobanner.php");
                $request = CommonFunc::getRequest();
		$modpromo = new ModelPromobanner($request);
		$modpromo->updatepromonewwindow($request);
		break;
		
	case base64_encode("delnews") :
		CommonFunc::loadFile('model/News.php');
		$request = CommonFunc::getRequest();
		$modNews = new ModelNews();
		$modNews->delNews($request);
		break;	
		
	case base64_encode("sapconnect") :
		CommonFunc::loadFile("controller/SettingsController.php");
		$clsSettings = new Settings();
		$clsSettings->connectSAPServer();
		break;	
	case base64_encode("processorder") :
		CommonFunc::loadFile('model/Order.php');
		$request = CommonFunc::getRequest();
		$modOrder = new ModelOrder();
		$modOrder->ProcessOrder($request);
		break;
	case base64_encode("loadbkfiles") :
		CommonFunc::loadFile("controller/SettingsController.php");
		$clsSettings = new Settings();
		$clsSettings->loadBackupFiles();
		break;
		
	case base64_encode("processbackup") :
		CommonFunc::loadFile("controller/SettingsController.php");
		$clsSettings = new Settings();
		$clsSettings->processBackup();
		break;	
		
	case base64_encode("paginator") :
		CommonFunc::getRequestGET("../library/adapters/Paginator.php");
		$clsPaginator = new PaginatorHelper();
		$request = CommonFunc::getRequest();
		$mod = $request['mod'];
		if($mod == 'product')
		{
			CommonFunc::loadFile("controller/ProductController.php");
			//$page = $request['page'];
			$clsProduct = new Product();
			$clsProduct->index();			
		}
		break;	
		
	case base64_encode("importrates") :
		CommonFunc::loadFile("controller/SettingsController.php");
		$clsSettings = new Settings();
		$clsSettings->importRates();
		break;
      
	case base64_encode("importrates2") :
		CommonFunc::loadFile("controller/SettingsController.php");
		$clsSettings = new Settings();
		$clsSettings->importRates2();
		break;
		
	case base64_encode("renamecategory") :
		CommonFunc::loadFile("controller/CategoryController.php");
		$clsCat = new Category();
		$clsCat->renameCategory();
		break;	
		
	default :
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		$smarty = new Smarty();
		$smarty->display(ADMINTPL . 'adminview.tpl');
		break;
            	break;
       case base64_encode("saveuser") :
		CommonFunc::loadFile('model/User.php');
		$request = CommonFunc::getRequest();
                //print_r($request);
		$modUser = new ModelUser();
		$modUser->saveUser($request);
		break;
}
?>